var j=0,prefix="/wxmaterial";
$(function(){
	 initUeditor(j);
	 layerUpload("right_banner_0",0);
})

function layerUpload(k,j){
    layui.use('upload', function () {
        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: '#'+k, //绑定元素
            url:prefix+'/uploadBanner', //上传接口
            size: 1000,
            accept: 'file',
            done: function (r) {
            	$("#left_img_"+j).attr("src",r.localurl);
            	$("#right_img_"+j).val(r.wxurl);
            },
            error: function (r) {
                layer.msg(r.msg);
            }
        });
    });
}



function removeArticle(kid){
	var index=layer.confirm('确定要删除该图文？', {
		  btn: ['确认','取消'] //按钮
		}, function(){
		  $("#left_article_"+kid).prev("hr").remove();
		  $("#left_article_"+kid).remove();
		  UE.getEditor('contents_'+kid).destroy();
		  $("#right_article_"+kid).remove();
		  $("#ueitorflag").prev("div").show();
		  layer.close(index);
		}, function(){
		});
}

function addArticle(obj){
	var c=$('div[id^=right_article_]').length;
	if(c>7){
		layer.msg("超出限制!");
		return;
	}
	j+=1;
	var str="";
	str+="<a href='#' id='left_article_"+j+"'>";
	str+="<div class='col-sm-3'>";
	str+="<div class='text-center'>";
	str+="<img id='left_img_"+j+"' alt='image' class='m-t-xs img-responsive' src='/images/a2.jpg'>";
	str+="<div class='m-t-xs font-bold'>CTO</div>";
	str+="</div>";
	str+="</div>";
	
	str+="<div class='col-sm-7'>";
	str+="<p id='left_title_"+j+"'>文章标题</p>";
	str+="</div>";
	
	str+="<div class='col-sm-1'>";
	str+="<div class='text-center'>";
	str+="<li onclick=removeArticle('"+j+"') class='fa fa-remove' style='font-size:30px'></li>";
	str+="</div>";
	str+="</div>";
	
	str+="<div class='clearfix' onclick='controllShow("+j+")'></div>";
	str+="</a>";
	str+=" <hr>";
	$(obj).before(str);
	addRight(j);
}

function addRight(n){
	var str="";
	str+="<div class='mail-body' id='right_article_"+n+"'>";
	str+="<div class='form-group'>";
	str+="<div class='col-sm-12'>";
	str+="<input type='text' name='title["+n+"]' id='right_title_"+n+"' oninput=synchronous(this,'left_title_"+n+"') class='form-control' placeholder='标题'>";
	str+="</div>";
	str+="</div>";
	str+="<div class='form-group'>";
	str+="<div class='col-sm-12'>";
	str+="<input type='text' name='author["+n+"]' class='form-control' value='' placeholder='作者'>";
	str+="</div>";
	str+="</div>";
	//封面上传吧
	str+="<div class='form-group'>";
	str+="<div class='col-sm-4'>";
	str+="<button type='button' id='right_banner_"+n+"' class='form-control' >封面上传</button>";
	str+="<input type='hidden' name='banner["+n+"]' id='right_img_"+n+"' class='form-control' value=''>";
	str+="</div>";
	str+="<div class='col-sm-8'>";
	str+="<input type='text' id='right_author_"+n+"' name='sourceUrl["+n+"]' class='form-control' value='' placeholder='原文链接'>";
	str+="</div>";
	str+="</div>";
	//end
	str+="<div class='form-group'>";
	str+="<div class='col-sm-12'>";
	str+="<script id='contents_"+n+"' name='content["+n+"]' type='text/plain'></script>";
	str+="</div>";
	str+="</div>";
	str+="</div>";
	$("#ueitorflag").before(str);
	initUeditor(n);
	controllShow(n);
	layerUpload("right_banner_"+n,n);
}

function synchronous(obj,id){
	var v=$(obj).val();
	if(v&&v.length>36){
		$("#"+id).text(v.substring(0,36)+"...");
	}else{
		$("#"+id).text(v);
	}
}

function initUeditor(k){
	  var vname="ue"+k;
	  var obj=UE.getEditor('contents_'+k);
	  window[vname]=obj;
	  obj.ready(function() {
		  obj.setHeight(420);
	  })
}

function controllShow(k){
	$('div[id^=right_article_]').each(function(i,item){
		if($(item).attr("id")=="right_article_"+k){
			$(item).show();
		}else{
			$(item).hide();
		}
	})
}


function controllHide(k){
	$('div[id^=right_article_]').each(function(i,item){
		if($(item).attr("id")=="right_article_"+k){
			$(item).show();
		}else{
			$(item).hide();
		}
	})
}

function save(){
	$("#saveForm").attr("action","/wxMaterialArticle/save");
	$("#saveForm").submit();
	console.log(formSerialize("saveForm"))
	
}